package com.jxust.vhremployee9092.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jxust.debate.pojo.Employee;
import com.jxust.debate.util.CommonResult;
import com.jxust.debate.util.CommonResult.Resultcode;
import com.jxust.debate.util.PageInfo;
import com.jxust.vhremployee9092.dao.EmployeeDao;
import com.jxust.vhremployee9092.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author tyeerth
 * @date 2020/12/27 - 14:48
 */
@Service
@Transactional
public class TestServiceImpl extends BaseServiceImpl<Employee> implements TestService {

    @Autowired
    private EmployeeDao employeeDao;
//    @Override
//    public List<DemoEntity> getAll() {
//        List<DemoEntity> demoEntities = super.queryAll();
//        return demoEntities;
//    }

    @Override
    public CommonResult<String> insertEmployee(Employee employee) {
        Integer save = super.update(employee);
//        Integer save = super.save(employee);
        if (save == 0) {
            return CommonResult.fail(Resultcode.FAIL, "保存用户信息失败");
        }
        return CommonResult.success("保存成功");
    }

    @Override
    public PageInfo<Employee> getAll(Employee employee, Integer pageNum, Integer pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper<>(employee);
        IPage<Employee> iPage = super.queryPageList(queryWrapper, pageNum, pageSize);
//        final IPage<Employee> employeeIPage = super.queryPageListByWhere(new Employee(), pageNum, pageSize);
        return new PageInfo<Employee>
                (Long.valueOf(iPage.getTotal()).intValue(), pageNum, pageSize, iPage.getRecords());
    }


    @Override
    public CommonResult<Employee> getEmployeeByUsername(Employee employee) {
        Employee employee1 = super.queryOne(employee);
//         Employee employee1 = employeeDao.selectByPrimaryKey("1");
        return CommonResult.success(employee1);
    }

    @Override
    public CommonResult<IPage<Employee>> selectPageExt(Employee employee, Integer pageNum, Integer pageSize) {
        return null;
    }

    @Override
    public CommonResult<String> registerEmployee(String username, String password) {
        Employee employee = new Employee();
        employee.setUsername(username);
        employee.setPassword(password);
        Employee employee1 = new Employee();
        employee1.setUsername(username);
        Employee employee2 = super.queryOne(employee1);
        if (employee2 != null) {
            return CommonResult.fail(Resultcode.FAIL, "注册失败,用户名已存在");
        }
        Integer save = super.save(employee);
        if (save != -1) {
            return CommonResult.success("注册成功");
        } else {
            return CommonResult.fail(Resultcode.FAIL, "注册失败");
        }
    }

    @Override
    public Employee getEmployee(String username) {
        Employee employee = new Employee();
        employee.setUsername(username);
        return super.queryOne(employee);
    }

}
